name: Build Gradle project
on:
  push:
jobs:
  build-gradle-project:
    runs-on: [self-hosted, linux, x64]
    steps:
      - name: Checkout project sources
        uses: actions/checkout@v3
      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 17
      - name: Setup Gradle
        uses: gradle/gradle-build-action@v2

      - name: Run Test Coverage
        run: ./gradlew jacocoTestReport
      - name: Generate JaCoCo Badge
        uses: cicirello/jacoco-badge-generator@v2
        with:
          generate-branches-badge: true
          jacoco-csv-file: build/reports/jacoco/test/jacocoTestReport.csv

      - name: Log coverage percentage
        run: |
          echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
          echo "branch coverage = ${{ steps.jacoco.outputs.branches }}"

      - name: Commit the badge (if it changed)
        run: |
          if [[ `git status --porcelain` ]]; then
            git config --global user.name 'Gordeev Egor'
            git config --global user.email 'SuperStep@users.noreply.github.com'
            git add -A
            git commit -m "Generated JaCoCo coverage badge"
            git push
          fi

      - name: Upload JaCoCo coverage report
        uses: actions/upload-artifact@v2
        with:
          name: jacoco-report
          path: target/site/jacoco/

#      - name: Run build with Gradle Wrapper
#        run: ./gradlew assemble